package org.example.settings.controller;

import org.example.commons.constant.Constant;
import org.example.commons.domain.ErrorInfo;
import org.example.commons.utils.DateFormatUtils;
import org.example.settings.domain.User;
import org.example.settings.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.util.Date;
import java.util.HashMap;

@Controller
@RequestMapping("/user")
public class UserController {

    @Autowired
    private UserService service;

    @RequestMapping("/findOne")
    @ResponseBody
    public Object findOne(HttpServletResponse response, HttpSession session, String login_act, String login_pwd, String isRember, HttpServletRequest request){
        HashMap<String, String> map = new HashMap<>();
        map.put("login_act",login_act);
        map.put("login_pwd",login_pwd);
        User user = service.findOne(map);
        ErrorInfo errorInfo = new ErrorInfo();
        if(user==null){
            errorInfo.setErrorCode(Constant.ERROR_INFO_CODE_FAIL);
            errorInfo.setErrorMessage("该用户不存在！");
        }else{
            String dateTime = DateFormatUtils.formatDateTime(new Date());
            if(dateTime.compareTo(user.getExpireTime()) > 0){
                errorInfo.setErrorCode(Constant.ERROR_INFO_CODE_FAIL);
                errorInfo.setErrorMessage("该用户已过期！");
            }else if ("0".equals(user.getLockState())){
                errorInfo.setErrorCode(Constant.ERROR_INFO_CODE_FAIL);
                errorInfo.setErrorMessage("该用户已被锁定！");
            }else if (!user.getAllowIps().contains(request.getRemoteAddr())){
                errorInfo.setErrorCode(Constant.ERROR_INFO_CODE_FAIL);
                errorInfo.setErrorMessage("IP不可用！");
            }else {
                errorInfo.setErrorCode(Constant.ERROR_INFO_CODE_SUCCESS);
                errorInfo.setErrorMessage("登录成功！");
                session.setAttribute(Constant.SESSION_USER,user);
                /*
                注意：只有加上setPath("/")语句才可以在项目的任何资源下得到所设置的cookie的值，
                否者就只能在设置cookie的哪个程序或接口才能拿到cookie
                比如本次项目的”/findOne“接口
                */
                if("true".equals(isRember)){
                    Cookie c1 = new Cookie("loginAct",user.getLoginAct());
                    c1.setMaxAge(10*24*60*60);
                    c1.setPath("/");
                    Cookie c2 = new Cookie("loginPwd",user.getLoginPwd());
                    c2.setMaxAge(10*24*60*60);
                    c2.setPath("/");
                    response.addCookie(c1);
                    response.addCookie(c2);
                }else{
                    Cookie c1 = new Cookie("loginAct","1");
                    c1.setMaxAge(0);
                    c1.setPath("/");
                    Cookie c2 = new Cookie("loginPwd","1");
                    c2.setMaxAge(0);
                    c2.setPath("/");
                    response.addCookie(c1);
                    response.addCookie(c2);
                }
            }
        }
        return errorInfo;
    }

    @RequestMapping("/safeExit")
    public String safeExit(HttpServletResponse response,HttpSession session){
        Cookie c1 = new Cookie("loginAct","1");
        c1.setMaxAge(0);
        c1.setPath("/");
        Cookie c2 = new Cookie("loginPwd","1");
        c2.setMaxAge(0);
        c2.setPath("/");
        response.addCookie(c1);
        response.addCookie(c2);
        session.invalidate();
        return "redirect:/";
    }
}
